package cc.javaee.bbs.controller.api;

import cc.javaee.bbs.constants.Constants;
import cc.javaee.bbs.dao.activity.BankuaiCustomerService;
import cc.javaee.bbs.exception.ResultException;
import cc.javaee.bbs.model.Bankuai;
import cc.javaee.bbs.model.Huifu;
import cc.javaee.bbs.model.Tiezi;
import cc.javaee.bbs.model.activity.BankuaiCustomer;
import cc.javaee.bbs.model.activity.KbrActivity;
import cc.javaee.bbs.model.activity.KbrActivityFile;
import cc.javaee.bbs.request.KoboroRequest;
import cc.javaee.bbs.service.BankuaiService;
import cc.javaee.bbs.service.HuifuService;
import cc.javaee.bbs.service.TieziService;
import cc.javaee.bbs.service.activity.ActivityService;
import cc.javaee.bbs.vo.CircleVO;
import cc.javaee.bbs.vo.ResultVO;
import cc.javaee.bbs.vo.TopicVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author xdw
 */
@RestController
public class CircleController {

    @Autowired
    private BankuaiService bankuaiService;
    @Autowired
    private TieziService tieziService;
    @Autowired
    private HuifuService huifuService;
    @Autowired
    private BankuaiCustomerService customerService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private BankuaiCustomerService bankuaiCustomerService;

    @RequestMapping("/circle/list")
    public ResultVO list(@RequestParam(value = "archivalNumber", defaultValue = "") String archivalNumber) {
        List<Bankuai> bankuaiList = bankuaiService.select(null);

        List<CircleVO> data = bankuaiList.stream().map(e -> CircleVO.conversionBankuai(e, archivalNumber))
                .collect(Collectors.toList());

        return ResultVO.success(data);
    }

    @RequestMapping("/circle/topic/list")
    public ResultVO topicList(@RequestParam("bankuaiId") Integer bankuaiId,
                              @RequestParam(value = "archivalNumber") String archivalNumber,
                              @RequestParam(value = "topicId", required = false) Integer topicId,
                              @RequestParam(value = "showCount", required = false) Integer showCount) {
        Bankuai bankuaiParam = new Bankuai();
        bankuaiParam.setId(bankuaiId);
        List<Bankuai> bankuaiList = bankuaiService.select(bankuaiParam);
        if (CollectionUtils.isEmpty(bankuaiList)) {
            return ResultVO.error(-1, "圈子不存在");
        }
        Bankuai bankuai = bankuaiList.get(0);

        final BankuaiCustomer customer;

        BankuaiCustomer bankuaiCustomer = customerService.findByArchivalNumber(archivalNumber);
        if (bankuaiCustomer == null) {
//            return ResultVO.error(-1, "未加入圈子");
            customer = new BankuaiCustomer();
            customer.setCustId(-1);
        } else {
            customer = bankuaiCustomer;
        }

        //获取圈子话题
        List<Tiezi> tieziList = tieziService.findTieZiByBankuaiid(bankuaiId, topicId);
        tieziList.stream();

        List<TopicVO> data = tieziList.stream().map(e -> {
            List<Huifu> tieziidList = huifuService.findByTieziid(e.getId(), customer.getCustId());

            if (CollectionUtils.isNotEmpty(tieziidList)) {
                // 获取投票记录      //1回复 2上传文件 3投票 4点赞
                long count = tieziidList.stream().filter(h -> h.getType() == 3).count();
                if (count > 0) {
                    e.setMyVoteStatus(1);
                }
                // 获取点赞         //1回复 2上传文件 3投票 4点赞
                long likeCount = tieziidList.stream().filter(h -> h.getType() == 4).count();
                if (likeCount > 0) {
                    e.setMyLikeStatus(1);
                }
            }
            KbrActivity activity = activityService.findById(e.getZhutiid());
            List<KbrActivityFile> actFileList = activity.getActFileList();
            if (CollectionUtils.isNotEmpty(actFileList)) {
                e.setFileImg(actFileList.get(0).getFilePath());
            }
            return TopicVO.conversionTopicVO(e, tieziidList);
        }).collect(Collectors.toList());

        //当前用户是否加入圈子
        BankuaiCustomer currentBankuaiCustomer = bankuaiCustomerService.getBankuaiCustomer(archivalNumber, bankuaiId);
        Map map = new HashMap();

        if (currentBankuaiCustomer != null) {
            map.put("myJoinStatus", 1);
        } else {
            map.put("myJoinStatus", 0);
        }


        data.forEach(e -> {
            List<TopicVO.Comment> commentList = e.getCommentList();
            if (CollectionUtils.isNotEmpty(commentList)) {
                if (showCount != null && showCount > 0) {
                    if (commentList.size() > showCount) {
                        List list = commentList.subList(0, showCount);
                        e.setCommentList(list);
                    }
                }
                commentList.forEach(c -> {
                    List<Huifu> byHuifuid = huifuService.getByHuifuid(c.getId(), 4);
                    if (CollectionUtils.isNotEmpty(byHuifuid)) {
                        long count = byHuifuid.stream().filter(b -> customer.getCustId().equals(b.getCreateuserid())).count();
                        c.setMyStatus(count > 0 ? 1 : 0);
                        c.setLikeCount(byHuifuid.size());
                    }
                });
            }
        });


        map.put("data", data);
        return ResultVO.success(map);

    }

    /**
     * @return
     */
    @RequestMapping("/circle/topic/operate")
    //投票点赞评论
    public ResultVO operate(Huifu huifu) {

//        huifu.

        huifuService.insert(huifu);

        Bankuai bankuaiParam = new Bankuai();
        List<Bankuai> bankuaiList = bankuaiService.select(bankuaiParam);
        if (CollectionUtils.isEmpty(bankuaiList)) {
            return ResultVO.error(-1, "圈子不存在");
        }
        Bankuai bankuai = bankuaiList.get(0);

        //获取圈子话题
        Tiezi tieziParam = new Tiezi();
//        tieziParam.setBankuaiId(bankuaiId);
        List<Tiezi> tieziList = tieziService.select(tieziParam);

        tieziList.stream();

//        List<TopicVO> data = tieziList.stream().map(e -> TopicVO.conversionTopicVO(e, archivalNumber))
//                .collect(Collectors.toList());

        return ResultVO.success();
    }

    /**
     * @return
     */
    @RequestMapping("/circle/add/{id}")
    public ResultVO operate(@PathVariable Integer id, @RequestParam("archivalNumber") String archivalNumber) {
        try {
            bankuaiCustomerService.joinBankuai(archivalNumber, id);
        } catch (ResultException e) {
            return ResultVO.error(e.getCode(), e.getMsg());
        }
        return ResultVO.success();
    }

    @RequestMapping("/post/add")
    public ResultVO add(Tiezi tiezi) {

        return ResultVO.success();
    }
}
